package com.bdc.utils;

import com.bdc.BdcVar;
import com.bdc.memory.domain.tools.MemoryTimeInfo;

import java.util.ArrayList;

/**
 * User: shawn
 * Date: 2011-6-22
 * Time: 21:18:57
 */
public class BdcUtils {

    public static float getEf(int times, float oldEf, int score) {
        boolean isFirstTime = times == BdcVar.TIMES_FIRST;
        boolean isSecondTime = times == BdcVar.TIMES_FIRST + 1;
        if (isFirstTime || isSecondTime) return BdcVar.EF_DEFAULT;

        double newEf = oldEf + (0.1 - (5 - score) * (0.08 + (5 - score) * 0.02));
        return newEf <= BdcVar.EF_MIN ? BdcVar.EF_MIN : (float) newEf;
    }

    public static MemoryTimeInfo getIntervalMemoryTime(int times, int intervalDays, int score, float ef) {
        boolean isFirstTime = times == BdcVar.TIMES_FIRST;
        boolean isSecondTime = times == BdcVar.TIMES_FIRST + 1;
        boolean isResetScore = score < BdcVar.SCORE_RESET;

        if (isFirstTime || isResetScore) {
            return new MemoryTimeInfo(BdcVar.TIMES_FIRST + 1, BdcVar.INTERVAL_FIRST);
        }
        else if (isSecondTime) {
            return new MemoryTimeInfo(BdcVar.TIMES_FIRST + 2, BdcVar.INTERVAL_SECOND);
        }
        else {
            return new MemoryTimeInfo(times + 1, (int) (intervalDays * ef));
        }
    }

    public static String filterTagString(String tagString) {
        ArrayList<String> tagList = new ArrayList<String>();

        String[] tagArray = TokenUtils.getStringArray(tagString, BdcVar.TAG_SEPERATOR);
        for (String tag : tagArray) {
            if (tagList.contains(tag)) continue;
            tagList.add(tag);
        }

        String result = "";
        for (String tag : tagList) {
            result += (result.length() == 0 ? "" : BdcVar.TAG_SEPERATOR) + tag;
        }

        return result;
    }
}
